package com.chuangjiangx.member.basic.ddd.domain.model;

import com.chuangjiangx.dddbase.Entity;
import com.chuangjiangx.domain.shared.model.Enable;
import com.chuangjiangx.domain.shared.model.Sex;
import com.chuangjiangx.member.coupon.ddd.domain.model.SwicthEnum;
import lombok.Getter;

import java.util.Date;

/**
 * 会员
 */
@Getter
public class Member extends Entity<MemberId> {

    /**
     * 会员卡号
     */
    private String memberCardNum;

    /**
     * 会员昵称
     */
    private String nickName;

    /**
     * 会员姓名
     */
    private String name;

    /**
     * 会员手机号
     */
    private String mobile;
    /**
     * 会员用户头像
     */
    private String headimgurl;

    /**
     * 会员性别
     */
    private Sex sex;

    /**
     * 会员生日
     */
    private Date birthday;

    /**
     * 注册时间
     */
    private Date registerTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 是否启用
     */
    private Enable enable;

    /**
     * 协议：同意 1  不同意 0
     */
    private AgreementType agreement;

    /**
     * 会员注册终端信息
     */
    private OperationInfo operationInfo;

    /**
     * 人脸Id
     */
    private String faceId;

    private SwicthEnum syncWxStatus;

    private Date syncWxTime;

    /**
     * 数据库持久化转换构造方法
     *
     * @param id            会员id
     * @param memberCardNum 会员卡号
     * @param nickName      会员昵称
     * @param name          会员名称
     * @param mobile        会员手机号码
     * @param sex           会员性别
     * @param birthday      会员生日
     * @param operationInfo 注册终端信息
     * @param registerTime  注册时间
     * @param updateTime    更新时间
     * @param enable        是否启用
     * @param agreement     协议：同意 1  不同意 0
     */
    public Member(MemberId id, String memberCardNum, String nickName, String name, String mobile,
                  Sex sex, Date birthday, Date registerTime, Date updateTime, Enable enable,
                  AgreementType agreement, OperationInfo operationInfo, String headimgurl, SwicthEnum syncWxStatus, Date syncWxTime, String faceId) {
        setId(id);
        this.memberCardNum = memberCardNum;
        this.nickName = nickName;
        this.name = name;
        this.mobile = mobile;
        this.sex = sex;
        this.birthday = birthday;
        this.registerTime = registerTime;
        this.updateTime = updateTime;
        this.enable = enable;
        this.agreement = agreement;
        this.operationInfo = operationInfo;
        this.headimgurl = headimgurl;
        this.faceId = faceId;
        this.syncWxStatus = syncWxStatus;
        this.syncWxTime = syncWxTime;
    }

    /**
     * 注册会员
     *
     * @param memberCardNum 会员卡号
     * @param name          会员姓名
     * @param mobile        会员手机号
     * @param birthday      会员生日
     * @param sex           会员性别
     */
    public Member(String memberCardNum, String name, String mobile,
                  String headimgurl,
                  Date birthday, Sex sex, OperationInfo operationInfo) {
        this.memberCardNum = memberCardNum;
        this.name = name;
        this.mobile = mobile;
        this.headimgurl = headimgurl;
        this.birthday = birthday;
        this.sex = sex;
        this.registerTime = new Date();
        this.operationInfo = operationInfo;
        this.enable = Enable.ENABLE;
        this.agreement = AgreementType.AGREE;
    }

    /**
     * 编辑会员信息
     *
     * @param name          姓名
     * @param sex           性别
     * @param birthday      生日
     * @param operationInfo 终端信息
     */
    public void modifyBasicInfo(String name, Sex sex, Date birthday, String headimgurl, OperationInfo operationInfo) {
        this.name = name;
        if (sex != null) {
            this.sex = sex;
        }
        this.birthday = birthday;
        this.headimgurl = headimgurl;
        this.updateTime = new Date();
        if (operationInfo != null) {
            this.operationInfo = operationInfo;
        }
    }

    /**
     * 修改会员手机
     *
     * @param mobile        手机号码
     * @param operationInfo 终端信息
     */
    public void modifyMobile(String mobile, OperationInfo operationInfo) {
        this.mobile = mobile;
        this.updateTime = new Date();
        this.operationInfo = operationInfo;
    }

    public void modifyMobile(String mobile) {
        this.mobile = mobile;
        this.updateTime = new Date();
    }

    public void modifyHeadimgurl(String headimgurl) {
        this.headimgurl = headimgurl;
        this.updateTime = new Date();
    }

    public void modifyName(String name) {
        this.name = name;
        this.updateTime = new Date();
    }
    public void modifySex(Sex sex) {
        this.sex = sex;
        this.updateTime = new Date();
    }

    public void modifyFaceId(String faceId) {
        this.faceId = faceId;
    }

    public boolean isEnable() {
        if (Enable.ENABLE.equals(this.enable)) {
            return true;
        }
        return false;
    }

    /**
     * 启用
     */
    public void enable() {
        if (this.enable != Enable.ENABLE) {
            this.enable = Enable.ENABLE;
            this.updateTime = new Date();
        }
    }

    /**
     * 禁用(删除)
     */
    public void disable(OperationInfo operationInfo) {
        if (this.enable != Enable.DISABLE) {
            this.enable = Enable.DISABLE;
            this.updateTime = new Date();
            this.operationInfo = operationInfo;
        }
    }

    public void syncWxStatus(SwicthEnum syncWxStatus) {
        this.syncWxStatus = syncWxStatus;
        this.syncWxTime = new Date();
    }
}